﻿$(function() {
    //Tied Navigation
    var nav = $("#topnav a");
    nav.each(function(i) { var dom = $(this); dom.click(function() { clicknav(nav, i); }); });

    //Point Navigation
    function clicknav(nav, id) {
        var li = 0;

        var next = new Array();
        next[0] = $("#leftnav ul,#leftnav div.post");
        next[1] = nav;
        for (var k = 0; k < 2; k++) {
            next[k].each(function(i) {
                var th = $(this);
                if (i == id) {
                    if (k == 0) {
                        var la = th.find("li a");
                        var url = la.eq(li).attr("href");
                        if (lang instanceof jQuery) url = lang.attr("href");
                        la.removeClass("on");
                        th.show();
                        if (url) {
                            $("#content div.iframe").append(loadingimg);
                            $('#loading').height($('#main').height());
                            la.eq(li).addClass("on");
                            $("#main").attr("src", url);
                        } else {
                            $("#main").attr("src", "");
                        }
                    }
                    if (k == 1) th.addClass("onnav");
                } else {
                    if (k == 0) {
                        var la = th.find("li a");
                        th.hide();
                        la.removeClass("on");
                    }
                    if (k == 1) th.removeClass("onnav");
                }
            });
        }
    }
    // to initial the left column for the first time
    firstLoad();
    function firstLoad() {
        var dom = $('.ajaxUpdate:first').trigger('click');
    }
    
})

function StringToObject(OldValue) {
    return eval("(" + OldValue + ")");
}
//-------------------------------------------------------------------------------------------------------------------

//extent the function of string
String.format = function() {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }
    return s;
};

Request = {
    QueryString: function(item) {
        var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
        return svalue ? svalue[1] : svalue;
    }
};


//-------------------------------------------------------------------------------------------------------------------
//Submite the json string to remote server
function SubmitChange(id, url) {//SubmitChange begin
    var dg = $('#' + id);
    var dgRows = dg.datagrid("getData");
    var dbCols = dg.datagrid("getColumnFields");

    var insertRows = dg.datagrid('getChanges', 'inserted');
    var updateRows = dg.datagrid('getChanges', 'updated');
    var deleteRows = dg.datagrid('getChanges', 'deleted');
    var changesRows = {
        Insert: [],
        Update: [],
        Delete: []
    };
    if (insertRows.length > 0) {
        for (var i = 0; i < insertRows.length; i++) {
            changesRows.Insert.push(insertRows[i]);
        }
    }

    if (updateRows.length > 0) {
        for (var k = 0; k < updateRows.length; k++) {
            changesRows.Update.push(updateRows[k]);
        }
    }

    if (deleteRows.length > 0) {
        for (var j = 0; j < deleteRows.length; j++) {
            changesRows.Delete.push(deleteRows[j]);
        }
    }

    var allData = JSON.stringify(changesRows);
   // allData = JSONtoXML(allData);
    
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: url,
        data: allData,
        beforeSend: function() {
//            alert(allData);
        },
        success: function(msg) {
            // refresh the datagrid               
            dg.datagrid('reload');
        },
        error: function(ex) {
            $.messager.alert('Warning', ex.responseText, 'warning');
        }
    })
} //SubmitChange end

//
function GetDataGridValue(dgID) {
    var dgView = $('#' + dgID);
    var dgRows = dgView.datagrid("getData");
    var dbCols = dgView.datagrid("getColumnFields");
    var allData = "";
    for (var i = 0; i < dgRows.rows.length; i++) {
        var oneRow = "";
        var oneColumn = "";

        if (i > 0)
            allData += ",";

        for (var j = 0; j < dbCols.length; j++) {
            if (j > 0)
                oneColumn += ",";
            var colName = dbCols[j];
            var value = dgRows.rows[i][colName];
            oneColumn += String.format("\"{0}\":\"{1}\"", colName, value);
        }
        oneRow = String.format("\{{0}\}", oneColumn);
        allData += oneRow;
    }
    allData = String.format("[{0}]", allData);
    return allData;
};

function GetDataGridTitle(dgID, startIndex) {
    var dgView = $('#' + dgID);
    var dbCols = dgView.datagrid("getColumnFields");
    var arrTitle = new Array(dbCols.length - startIndex);

    for (var i = startIndex; i < dbCols.length; i++) {
        var opts = dgView.datagrid("getColumnOption", dbCols[i]);
        arrTitle[i - startIndex] = opts.title;
    }

    return arrTitle.toString();
};

function GetDataGridField(dgID, startIndex) {
    var dgView = $('#' + dgID);
    var dbCols = dgView.datagrid("getColumnFields");
    var arrField = new Array(dbCols.length - startIndex);

    for (var i = startIndex; i < dbCols.length; i++) {
        arrField[i - startIndex] = dbCols[i];
    }
    return arrField.toString();
};

function OBJtoXML(obj, d) {
    d = (d) ? d : 0;
    var rString = "\n";
    var pad = "";
    for (var i = 0; i < d; i++) {
        pad += " ";
    }
    if (typeof obj === "object") {
        if (obj.constructor.toString().indexOf("Array") !== -1) {
            for (i = 0; i < obj.length; i++) {
                rString += pad + "<item>" + obj[i] + "</item>\n";
            }
            rString = rString.substr(0, rString.length - 1)
        }
        else {
            for (i in obj) {
                var val = OBJtoXML(obj[i], d + 1);
                if (!val)
                    return false;
                rString += ((rString === "\n") ? "" : "\n") + pad + "<" + i + ">" + val + ((typeof obj[i] === "object") ? "\n" + pad : "") + "</" + i + ">";
            }
        }
    }
    else if (typeof obj === "string") {
        rString = obj;
    }
    else if (obj.toString) {
        rString = obj.toString();
    }
    else {
        return false;
    }
    return rString;
}

function JSONtoXML(json) {
    return eval("OBJtoXML(" + json + ");");
}
